﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0812045
{
    public class TankSprite : BaseSprite
    {
        public TankSprite()
        {
        }
        public TankSprite(Texture2D[] texture2D, Texture2D track)
        {
            _texture2d = texture2D;
            _itexture2d = 0;
            _ntexture2d = 10;
            _x = 0;
            _y = 480 - texture2d[0].Height;
            tracks = track;
            pointToDrawTrack.Y = y + 35;
            pointToDrawTrack.X = x - 574;
        }
        int ndelay;
        int delay = 0;
        private Texture2D tracks;
        int tocDo;
        Vector2 pointToDrawTrack = new Vector2();
        public override void Update(GameTime gameTime)
        {
            MouseState ms = Mouse.GetState();

            int khoangCach = Math.Abs(ms.X - (int)_x - 40);
            tocDo = 0;
            if (khoangCach > 400)
            {
                if (_x + 40 < ms.X)
                    tocDo = 5;
                if (_x + 40 > ms.X)
                    tocDo = -5;
            }
            else if (khoangCach > 300)
            {
                if (_x + 40 < ms.X)
                    tocDo = 4;
                if (_x + 40 > ms.X)
                    tocDo = -4;
            }
            else if (khoangCach > 100)
            {
                if (_x + 40 < ms.X)
                    tocDo = 3;
                if (_x + 40 > ms.X)
                    tocDo = -3;
            }
            else if (khoangCach > 50)
            {
                if (_x + 40 < ms.X)
                    tocDo = 2;
                if (_x + 40 > ms.X)
                    tocDo = -2;
            }
            else
            {
                if (_x + 40 < ms.X)
                    tocDo = 1;
                if (_x + 40 > ms.X)
                    tocDo = -1;
            }
            _x += tocDo;
            if (tocDo >= 0)
            {
                if (pointToDrawTrack.X + 574 <= _x)
                    pointToDrawTrack.X = _x - 574;
            }
            pointToDrawTrack.X--;
            if (tocDo != -1)
            {
                if (delay == 0)
                {
                    _itexture2d = (_itexture2d + 1) % _ntexture2d;
                }
            }
            delay = (delay + 1) % 3;
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture2d[0], new Rectangle((int)_x, (int)_y, 80, 55), new Rectangle(_itexture2d * 80, 0, 80, 55), Color.White);
            spriteBatch.Draw(tracks, pointToDrawTrack, Color.White);
        }
    }
}
